IOrders.models.Client = Ext.regModel('Client', {
    fields: ['id', 'name', 'address', 'warehouse'],
    proxy: {
        type: 'IOrders.proxy.clientDB'
    }
});

IOrders.models.Order = Ext.regModel('SaleOrder', {
    fields: ['id', 'toDate', 'isDocuments', 'isCredit', 'totalPrice', 'customer', 'customer_id'],
    hasMany: {
    	name: 'positions', model: 'Product',
    	storeConfig: {
            sorters: 'firstName',
            getGroupString : function(record) {
                return record.get('firstName');
            },
            sync: function(params) {
                var me        = this,
                    options   = {},
                    toCreate  = me.getNewRecords(),
                    toUpdate  = me.getUpdatedRecords(),
                    toDestroy = me.getRemovedRecords(),
                    needsSync = false;   
                if (toCreate.length > 0) {
                    options.create = {recs: toCreate, params: params};
                    needsSync = true;
                }    
                if (toUpdate.length > 0) {
                    options.update = {recs: toUpdate, params: params};
                    needsSync = true;
                }    
                if (toDestroy.length > 0) {
                    options.destroy = {recs: toDestroy, params: params};
                    needsSync = true;
                }    
                if (needsSync && me.fireEvent('beforesync', options) !== false) {
                    me.proxy.batch(options, me.getBatchListeners());
                }
            }
    	}
    },
    proxy: {
        type: 'IOrders.proxy.orderDB'
    }
});

IOrders.models.ProductCategory = Ext.regModel('ProductCategory', {
    fields: ['id', 'name', 'isInactive', 'packageName', 'totalPrice'],
    hasMany: {
        name: 'products', model: 'Product',
        storeConfig: {
            sorters: 'firstName',
            getGroupString : function(record) {
                return record.get('firstName');
            },
            sync: function(params) {
                var me        = this,
                    options   = {},
                    toCreate  = me.getNewRecords(),
                    toUpdate  = me.getUpdatedRecords(),
                    toDestroy = me.getRemovedRecords(),
                    needsSync = false;   
                if (toCreate.length > 0) {
                    options.create = {recs: toCreate, params: params};
                    needsSync = true;
                }    
                if (toUpdate.length > 0) {
                    options.update = {recs: toUpdate, params: params};
                    needsSync = true;
                }    
                if (toDestroy.length > 0) {
                    options.destroy = {recs: toDestroy, params: params};
                    needsSync = true;
                }    
                if (needsSync && me.fireEvent('beforesync', options) !== false) {
                    me.proxy.batch(options, me.getBatchListeners());
                }
            }
        }
    },
    proxy: {
        type: 'IOrders.proxy.productCategoryDB'
    }
});

IOrders.models.Product = Ext.regModel('Product', {
    fields: ['id', 'isActive', 'dateSinceOrder', 'firstName', 'name', 'level', 'price', 'factor', 'volume', 'productcategory_id', 'saleorder_id'],
    associations: [
    	{xtype: 'belongsTo', model: 'ProductCategory'},
    	{xtype: 'belongsTo', model: 'SaleOrder'}
    ],
    proxy: {
        type: 'IOrders.proxy.orderPositionDB'
    }
});